<?php

namespace app\api\service;

//自定义收费服务
use app\model\CustomRechargeTemplate;
use app\model\Link;
use app\model\MountedAppletCustomRechargeConfig;
use app\model\VideoPayConfig;

class CustomChargeService
{
    private $linkModel;
    private $videoPayConfigModel;
    private $customRechargeTemplateModel;
    private $mountedAppletCustomRechargeConfigModel;

    public function __construct(
        Link $linkModel,
        VideoPayConfig $videoPayConfigModel,
        CustomRechargeTemplate $customRechargeTemplateModel,
        MountedAppletCustomRechargeConfig $mountedAppletCustomRechargeConfigModel
    ){
        $this->linkModel = $linkModel;
        $this->videoPayConfigModel = $videoPayConfigModel;
        $this->customRechargeTemplateModel = $customRechargeTemplateModel;
        $this->mountedAppletCustomRechargeConfigModel = $mountedAppletCustomRechargeConfigModel;
    }

    /**
     * 通过链接获取收费配置
     * @param $link_id
     * @param $video_id
     * @return array
     */
    public function getLinkConfigure($link_id, $video_id): array
    {
        $videoPayConf = []; //固定K币收取配置
        $rechargeInterval = []; //区间k币收取
        $newRechargeConf = 0;   //新版自定义充值 1否 2是
        $linkData = $this->linkModel->getInfoByWhere(
            ['id' => $link_id, 'video_id' => $video_id],
            'video_pay_setting_id, is_new_recharge_setting, recharge_template_id'
        );
        if (!$linkData) {
            return [$videoPayConf,$rechargeInterval,$newRechargeConf];
        }

        //是否是新版自定义充值 1否 2是
        if ($linkData['is_new_recharge_setting'] == 1) {
            $newRechargeConf = 1;
            if (!empty($linkData['video_pay_setting_id'])) {
                $videoPayConf = $this->videoPayConfigModel->getInfoByWhere(
                    ['id' => $linkData['video_pay_setting_id']],
                    'sort, status, pay_amount'
                );
            }
        } else {
            $newRechargeConf = 2;
            if (!empty($linkData['recharge_template_id'])) {
                $videoPayConf = $this->customRechargeTemplateModel->getInfoByWhere(
                    ['id' => $linkData['recharge_template_id'], 'video_id' => $video_id],
                    'sort, charge_type, pay_amount, interval_recharge'
                );
                if (!empty($videoPayConf) && $videoPayConf['charge_type'] == 2) {
                    $rechargeInterval = json_decode($videoPayConf['interval_recharge'], true);
                }
            }
        }

        return [$videoPayConf,$rechargeInterval,$newRechargeConf];
    }

    /**
     * 通过挂载获取收费配置
     * @param $mountPlatform
     * @param $video_id
     * @return array
     */
    public function getMountConfigure($mountPlatform, $video_id): array
    {
        $rechargeInterval = []; //区间K币收取配置
        $mountCustomChargeConf = $this->mountedAppletCustomRechargeConfigModel->getInfoByWhere(
            ['platform' => $mountPlatform, 'video_id' => $video_id],
            'sort, pay_amount, is_new_version, recharge_template_id'
        );

        if (!$mountCustomChargeConf || empty($mountCustomChargeConf['is_new_version'])) {
            return [$mountCustomChargeConf,$rechargeInterval];
        }

        $isNewVersion = $mountCustomChargeConf['is_new_version'];
        $mountCustomChargeConf = $this->customRechargeTemplateModel->getInfoByWhere(
            ['id' => $mountCustomChargeConf['recharge_template_id'], 'video_id' => $video_id],
            'sort, charge_type, pay_amount, interval_recharge'
        );

        if (!$mountCustomChargeConf) {
            return [$mountCustomChargeConf, $rechargeInterval];
        }

        $mountCustomChargeConf['is_new_version'] = $isNewVersion;

        if ($mountCustomChargeConf['charge_type'] == 2) {
            $rechargeInterval = json_decode($mountCustomChargeConf['interval_recharge'], true);
        }

        //$mountCustomChargeConf 区间K币收取
        return [$mountCustomChargeConf, $rechargeInterval];
    }

    /**
     * 处理固定收费配置
     * @param $intervalConf
     * @param $sort
     * @param $originIsPay
     * @param $originPayAmount
     * @return array
     */
    public function handleIntervalRecharge($intervalConf, $sort, $originIsPay, $originPayAmount): array
    {
        $datum = [];
        foreach ($intervalConf as $value) {
            if ($sort >= $value['start'] && $sort <= $value['end']) {
                $datum['is_pay'] = 1;
                $datum['pay_amount'] = $value['pay_amount'];
            } else if ($sort > $value['end']) {
                $datum['is_pay'] = $originIsPay;
                $datum['pay_amount'] = $originPayAmount;
            } else if ($sort < $intervalConf[0]['start']) {
                $datum['is_pay'] = 0;
                $datum['pay_amount'] = 0;
            }
        }
        return $datum;
    }
}